﻿
#include "bb_api.h"
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "Windows.h"


static uint32_t plot_count = 0;
static void     plot_callback(void* arg, void* user)
{
    bb_event_plot_data_t* data = (bb_event_plot_data_t*)arg;

    printf("user %u:\n", data->user);
    for (int i = 0; i < data->plot_num; i++) {
        bb_plot_data_t* item = &data->plot_data[i];
        printf("SNR: %u  LDPC: %u/%u GAIN:(%u, %u) MCS: %d FCH: %u\n",
               item->snr,
               item->ldpc_err,
               item->ldpc_num,
               item->gain_a,
               item->gain_b,
               item->mcs_rx,
               item->fch_lock);
    }
    plot_count += data->plot_num;
}

static void sense_test(bb_dev_handle_t* handle, uint32_t freq_khz, bb_rf_path_e rf_path)
{
    // 设置信号发生器的发送频点
    char cmdbuff[1024];
    sprintf(cmdbuff, "python signal_generator.py %d %d", freq_khz / 1000, -70);
    system(cmdbuff);

    // set rf path
    bb_set_rx_path_t rx_path;
    rx_path.path_bmp = 1 << BB_RF_PATH_A;
    bb_ioctl(handle, BB_SET_RX_PATH, &rx_path, NULL);

    // set rx freq
    bb_set_freq_t set_freq;
    set_freq.user     = BB_USER_BR_CS;
    set_freq.dir_bmp  = 1 << BB_DIR_RX;
    set_freq.freq_khz = freq_khz;
    bb_ioctl(handle, BB_SET_FREQ, &set_freq, NULL);

    // reset trx
    bb_set_reset_t set_reset;
    set_reset.user    = BB_USER_MAX;
    set_reset.dir_bmp = (1 << BB_DIR_TX) | (1 << BB_DIR_RX);
    bb_ioctl(handle, BB_SET_RESET, &set_reset, NULL);

    bb_set_plot_t plot;
    plot.user      = BB_USER_BR_CS;
    plot.enable    = 1;
    plot.cache_num = 3;
    plot_count     = 0;
    bb_ioctl(handle, BB_SET_PLOT, &plot, NULL);

    while (plot_count < 300) {
        Sleep(100);
    }

    plot.enable = 0;
    bb_ioctl(handle, BB_SET_PLOT, &plot, NULL);
}

int test_once()
{
    bb_host_t* phost;
    int        dev_nr;
    bb_dev_t** devs;
    bb_dev_handle_t* handle;
    // 2. 连接daemon
    if (bb_host_connect(&phost, "127.0.0.1", 50000)) {
        printf("failed to connect host!\n");
        return -1;
    }

    // 3. 获取设备列表
    if ((dev_nr = bb_dev_getlist(phost, &devs)) <= 0) {
        printf("get dev list failed or no dev!\n");
        return -1;
    }

    // 4. 打开其中一个
    handle = bb_dev_open(devs[0]);

    // 5. 释放列表
    bb_dev_freelist(devs);

    if (!handle) {
        printf("open dev failed!\n");
        return -1;
    }

    bb_set_event_callback_t callback;
    callback.event    = BB_EVENT_PLOT_DATA;
    callback.callback = plot_callback;
    callback.user     = 0;
    bb_ioctl(handle, BB_SET_EVENT_SUBSCRIBE, &callback, NULL);

    // bb_set_plot_t set_plot;
    // set_plot.enable = 1;
    // set_plot.user = BB_USER_BR_CS;
    // set_plot.cache_num = 3;

    // get chip id
    bb_get_status_in_t  status_in;
    bb_get_status_out_t status_out;
    status_in.user_bmp = 0x3fff;
    bb_ioctl(handle, BB_GET_STATUS, &status_in, &status_out);

    // get sdk verion
    bb_get_sys_info_out_t sys_info;
    bb_ioctl(handle, BB_GET_SYS_INFO, NULL, &sys_info);

    // enter sense mode
    bb_ioctl(handle, BB_SET_SENSE_TEST_MODE, NULL, NULL);
    Sleep(1000);

    // debug mode
    bb_set_dbg_mode_t dbg_mode;
    dbg_mode.enable = 1;
    bb_ioctl(handle, BB_SET_DBG_MODE, &dbg_mode, NULL);

    // 2、5G A/B PATH test
    sense_test(handle, 2390000, BB_RF_PATH_A);
    sense_test(handle, 2390000, BB_RF_PATH_B);
    sense_test(handle, 5880000, BB_RF_PATH_A);
    sense_test(handle, 5880000, BB_RF_PATH_B);

    // bb_ioctl(handle, BB_SET_PLOT, &set_plot, NULL);
    // Sleep(100);
    // set_plot.enable = 0;
    // bb_ioctl(handle, BB_SET_PLOT, &set_plot, NULL);

    // Sleep(3000);

    bb_dev_close(handle);
    bb_host_disconnect(phost);
    return 0;
}

int main()
{
    while (1) {
        test_once();
    }
    return 0;
}
